package idz.jsf;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;

@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean implements Serializable
{
	private static final long serialVersionUID = -1483720026257812677L;
	@ManagedProperty(value = "#{dataBean}")
	private DataBean data;
	private String ajaxNick;
	
	private String nick;
	private String firstname;
	private String lastname;
	private String email;
	private String password;
	private String confirmedpassword;
	private String description;
	private Timestamp creationdate;
	
	public void create ()
	{	 
		data.registerUser (new User (nick, password, firstname, lastname, email, description, creationdate));
		data.redirect ("log_in.xhtml");
		setNick("");
		setFirstname("");
		setLastname("");
		setEmail("");
		setPassword("");
		setConfirmedpassword("");
		setDescription("");
	}
	
	public void checkNick (AjaxBehaviorEvent e)
	{
		if (data.checkLogin(nick))
			this.ajaxNick = "";
		else
			this.ajaxNick = "Podany nick już istnieje!";	
	}
	
	public void setData(DataBean data)
	{
		this.data = data;
	}
	
	public String getAjaxNick()
	{
		return ajaxNick;
	}

	public void setAjaxNick(String ajaxNick)
	{
		this.ajaxNick = ajaxNick;
	}

	public String getNick() {
		return nick;
	}
	public void setNick(String nick) {
		this.nick = nick;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getConfirmedpassword() {
		return confirmedpassword;
	}
	public void setConfirmedpassword(String confirmedpassword) {
		this.confirmedpassword = confirmedpassword;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Timestamp getCreationdate() {
		return creationdate;
	}
	public void setCreationdate(Timestamp creationdate) {
		//this.creationdate = creationdate;
		this.creationdate = new Timestamp(new Date().getTime());
	}
}
